﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Utility;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCust_ProdCatDAL:Iapp_tblCust_ProdCat
    {
        HKEA_CMSEntities model;
        app_tblCust_ProdCat cust_procat;
        private app_tblCust_ProdCatDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCust_ProdCatDAL GetInstance()
        {
            return new app_tblCust_ProdCatDAL();
        }

        public int InsertENT(app_tblCust_ProdCat ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCust_ProdCat.AddObject(ent);
            model.SaveChanges();
            ent = model.app_tblCust_ProdCat.Where(a => a.AppCust_ProdCatUID > 0).OrderBy(a => a.AppCust_ProdCatUID).ToList().Last();
            return Convert.ToInt32(ent.AppCust_ProdCatUID);
        }
        public List<app_tblCust_ProdCat> GetListByENT(app_tblCust_ProdCat ent, PageParam param, string strPropertyName, string strOrderBy)
        {
            var result = model.app_tblCust_ProdCat.Where(a => a.AppCust_ProdCatUID > 0);
            if (ent.Cust_ProdCatUID > 0)
                result = result.Where(a => a.Cust_ProdCatUID == ent.Cust_ProdCatUID);
            if (ent.ProductCatUID > 0)
                result = result.Where(a => a.ProductCatUID == ent.ProductCatUID);
            if (ent.ProductSubCatUID > 0)
                result = result.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<app_tblCust_ProdCat> GetListByENT(app_tblCust_ProdCat ent)
        {
            var result = model.app_tblCust_ProdCat.Where(a => a.AppCust_ProdCatUID > 0);
            if (ent.Cust_ProdCatUID > 0)
                result = result.Where(a => a.Cust_ProdCatUID == ent.Cust_ProdCatUID);
            if (ent.ProductCatUID > 0)
                result = result.Where(a=>a.ProductCatUID==ent.ProductCatUID);
            if (ent.ProductSubCatUID > 0)
                result = result.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(app_tblCust_ProdCat ent)
        {
            var resule = model.app_tblCust_ProdCat.Where(a => a.AppCust_ProdCatUID > 0);
            if (ent.Cust_ProdCatUID > 0)
                resule = resule.Where(a => a.Cust_ProdCatUID == ent.Cust_ProdCatUID);
            if (ent.ProductCatUID > 0)
                resule = resule.Where(a => a.ProductCatUID == ent.ProductCatUID);
            if (ent.ProductSubCatUID > 0)
                resule = resule.Where(a=>a.ProductSubCatUID==ent.ProductSubCatUID);
            if (ent.CustomerUID > 0)
                resule = resule.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Deleted >= 0)
                resule = resule.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                resule = resule.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return resule.Count();
        }
        public int UpdateENT(app_tblCust_ProdCat ent)
        {
            cust_procat = model.app_tblCust_ProdCat.Where(a => a.CustomerUID == ent.CustomerUID && a.ProductSubCatUID == ent.ProductSubCatUID).FirstOrDefault();
            if (cust_procat != null)
            {
                cust_procat.Deleted = ent.Deleted;
                cust_procat.LastModifyBy = ent.LastModifyBy;
                cust_procat.LastModifyDate = DateTime.Now;
                cust_procat.Cust_ProdCatUID = ent.Cust_ProdCatUID;
                cust_procat.ProductCatUID = ent.ProductCatUID;
                cust_procat.ProductSubCatUID = ent.ProductSubCatUID;
                cust_procat.CustomerUID = ent.CustomerUID;
                cust_procat.Remark = ent.Remark;
            }
            return model.SaveChanges();
        } 
        public int DeleteENT(app_tblCust_ProdCat ent)
        {
            int rel = 0;
            var tmp = model.app_tblCust_ProdCat.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID && a.CustomerUID == ent.CustomerUID).ToList();
            foreach (app_tblCust_ProdCat item in tmp)
            {
                model.app_tblCust_ProdCat.DeleteObject(item);
                rel = model.SaveChanges();
            }
            return Convert.ToInt32(rel);
        }

        public List<int> GetCustomerUIDList()
        {
            return model.app_tblCust_ProdCat.Where(a => a.CustomerUID > 0).Select(a =>a.CustomerUID).ToList();
        }
    }
}
